_pyname=rst2pdf
_realname=rst2pdf
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.93
pkgrel=2
pkgdesc="Create PDFs from simple text markup, no LaTeX required (mingw-w64)"
arch=('any')
url='https://github.com/ralsina/rst2pdf'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-docutils"
             "${MINGW_PACKAGE_PREFIX}-python2-pdfrw"
             "${MINGW_PACKAGE_PREFIX}-python2-pygments" 
             "${MINGW_PACKAGE_PREFIX}-python2-reportlab>=2.4" 
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-docutils"
             "${MINGW_PACKAGE_PREFIX}-python3-pdfrw"
             "${MINGW_PACKAGE_PREFIX}-python3-pygments" 
             "${MINGW_PACKAGE_PREFIX}-python3-reportlab>=2.4"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/${_realname}/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('37e99ea103790321b29b6f6b0192821ba9c2990a6aec57295585ca6919932a97')

prepare() {  
  cd "$srcdir"/  
  for builddir in python{2,3}-build; do  
    rm -rf $builddir | true
    cp -r "${_pyname}-${pkgver}" "${builddir}"
  done  
}  
  
check() {  
  for pver in {2,3}; do  
    msg "Python ${pver} test for ${CARCH}"  
    cd "${srcdir}/python${pver}-build"  
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done  
  ${MINGW_PREFIX}/bin/2to3 -w "${srcdir}/python3-build"
}

package_python3-rst2pdf() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-docutils"
           "${MINGW_PACKAGE_PREFIX}-python3-pdfrw"
           "${MINGW_PACKAGE_PREFIX}-python3-pygments" 
           "${MINGW_PACKAGE_PREFIX}-python3-reportlab>=2.4" 
           "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python2-rst2pdf")
  # just in case some package wants to use the rst2pdf executable. 
  provides=("${MINGW_PACKAGE_PREFIX}-rst2pdf")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  
  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE.txt"

  # fix python command in files
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i rst2pdf-script.py
}

package_python2-rst2pdf() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-docutils"
           "${MINGW_PACKAGE_PREFIX}-python2-pdfrw"
           "${MINGW_PACKAGE_PREFIX}-python2-pygments" 
           "${MINGW_PACKAGE_PREFIX}-python2-reportlab>=2.4" 
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-rst2pdf")
  # just in case some package wants to use the rst2pdf executable. 
  provides=("${MINGW_PACKAGE_PREFIX}-rst2pdf")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  
  cd "${srcdir}/python2-build"
  
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1
  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE.txt"
  
  # fix python command in files
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i rst2pdf-script.py
}

package_mingw-w64-i686-python2-rst2pdf() {
  package_python2-rst2pdf
}

package_mingw-w64-i686-python3-rst2pdf() {
  package_python3-rst2pdf
}

package_mingw-w64-x86_64-python2-rst2pdf() {
  package_python2-rst2pdf
}

package_mingw-w64-x86_64-python3-rst2pdf() {
  package_python3-rst2pdf
}
